package com.bitjiuyeke;

import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ResourceBundle;
import java.util.Scanner;


public class Demo03_DataSource {
    public static void main(String[] args) {
        //定义mysql数据源
        MysqlDataSource mysqlDataSource = new MysqlDataSource();
        //设置数据库的连接串
        mysqlDataSource.setURL("jdbc:mysql://localhost:3306/java116");
        //设置用户名和密码
        mysqlDataSource.setUser("root");
        mysqlDataSource.setPassword("123456");
        //定义jdbc数据源对象
        DataSource dataSource = mysqlDataSource;

        //声明数据库连接对象
        Connection connection = null;
        //声明sql语句预处理对象
        PreparedStatement statement = null;
        //声明查询结果集对象
        ResultSet resultSet = null;

        try {//1获取数据库连接
            connection = dataSource.getConnection();
            //2定义sql语句
            String sql = "select id ,name,sno,age,gender,enroll_date , class_id  from student where name=? "; // 占位符替换
            // 接收用户输入的姓名
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要查询的学生姓名：");
            String inputname = scanner.next();
            scanner.close(); // 关闭 Scanner

            // 3获取sql预处理对象
            statement = connection.prepareStatement(sql);
            // 4用真实的姓名替换占位符
            statement.setString(1, inputname); // 占位符的index从1开始
            // 5执行sql语句
            resultSet = statement.executeQuery();
            // 获取查询结果集
            // 6遍历一条结果集，使用if语句
            if (resultSet.next()) { // resultSet.next()返回true则有下一行数据，false则没有
                // 获取数据列
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String sno = resultSet.getString("sno");
                int age = resultSet.getInt("age");
                Boolean gender = "M".equals(resultSet.getString("gender")); // 假设 'M' 代表男性，'F' 代表女性
                Date enroll_date = resultSet.getDate("enroll_date");
                int class_id = resultSet.getInt("class_id");
                // 输出数据
                System.out.println("id:" + id + ",name:" + name + ",sno:" + sno + ",age:" + age + ",gender:" + gender + ",enroll_date:" + enroll_date + ",class_id:" + class_id);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            // 7关闭资源
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

